﻿using DatabaseUserTest;
using DatabaseUserTest.Controller;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcfService.WCFModels;

namespace WcfService
{
    public class AdminService : IAdminService
    {
        private DestinationCTRL destCtrl = new DestinationCTRL();
        private RouteCTRL routeCtrl = new RouteCTRL();
        private PlaneinteriorCTRL interiorCtrl = new PlaneinteriorCTRL();
        private PlaneCTRL planeCtrl = new PlaneCTRL();

        #region Destination methods
        public List<WCFDestination> FindAllDestinations()
        {
            List<Destination> destList = destCtrl.FindAllDestinations();
            List<WCFDestination> wcfDestList = new List<WCFDestination>();
            foreach (Destination item in destList)
            {
                wcfDestList.Add(ConvertDestinationToWCFDestination(item));
            }
            return wcfDestList;

        }

        public WCFDestination FindDestinationName(string name)
        {
            return ConvertDestinationToWCFDestination(destCtrl.FindDestinationName(name));
        }

        public bool AddDestination(string name, string country)
        {
            if (destCtrl.AddDestination(name, country))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region Route methods
        public List<WCFRoute> FindAllRoutes()
        {
            List<Route> routeList = routeCtrl.FindAllRoutes();
            List<WCFRoute> wcfRouteList = new List<WCFRoute>();

            foreach (Route item in routeList)
            {
                wcfRouteList.Add(ConvertRouteToWCFRoute(item));
            }
            return wcfRouteList;
        }

        public bool AddRoute(string fromDestination, string toDestination, string price, string time)
        {
            if (routeCtrl.AddRoute(fromDestination, toDestination, price, time))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public WCFRoute FindRoute(string from, string to)
        {
            return ConvertRouteToWCFRoute(routeCtrl.FindRoute(from, to));
        }

        #endregion

        #region Interior methods
        public List<WCFPlaneInterior> FindAllPlaneInteriors()
        {
            List<WCFPlaneInterior> wcfPIList = new List<WCFPlaneInterior>();
            List<PlaneInterior> pIList = interiorCtrl.FindAllPlaneInteriors();

            foreach (PlaneInterior item in pIList)
            {
                wcfPIList.Add(ConvertPlaneInteriorToWCFPlaneInterior(item));
            }
            return wcfPIList;
        }

        public WCFPlaneInterior FindPlaneInterior(int id)
        {
            return ConvertPlaneInteriorToWCFPlaneInterior(interiorCtrl.FindPlaneInterior(id));
        }

        public bool AddPlaneInterior(string model, string firstclassseats, string normalseats)
        {
            if (interiorCtrl.AddPlaneInterior(model, firstclassseats, normalseats))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region Plane methods
        public bool AddPlane(string interiorId, string gate, DateTime departure, int routeId)
        {
            if (planeCtrl.AddPlane(interiorId, gate, departure, routeId))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region Convert methods

        private WCFDestination ConvertDestinationToWCFDestination(Destination destination)
        {
            WCFDestination wcfDestination = new WCFDestination();
            wcfDestination.DestinationId = destination.DestinationId;
            wcfDestination.Name = destination.Name;
            wcfDestination.Country = destination.Country;
            return wcfDestination;

        }

        private Destination ConvertWCFDestinationToDestination(WCFDestination wcfDestination)
        {
            Destination destination = new Destination();
            destination.DestinationId = wcfDestination.DestinationId;
            destination.Name = wcfDestination.Name;
            destination.Country = wcfDestination.Country;
            return destination;
        }

        private Plane ConvertWCFPlaneToPlane(WCFPlane wcfPlane)
        {
            Plane plane = new Plane();
            plane.PlaneId = wcfPlane.PlaneId;
            plane.Gate = wcfPlane.Gate;
            plane.Departure = wcfPlane.Departure;
            plane.Arrival = wcfPlane.Arrival;
            plane.InteriorId = wcfPlane.InteriorId;
            plane.ReservedFirstClassSeats = wcfPlane.ReservedFirstClassSeats;
            plane.ReservedNormalSeats = wcfPlane.ReservedNormalSeats;
            plane.RouteId = wcfPlane.RouteId;
            return plane;
        }

        private WCFPlane ConvertPlaneToWCFPlane(Plane plane)
        {
            WCFPlane wcfPlane = new WCFPlane();
            wcfPlane.PlaneId = plane.PlaneId;
            wcfPlane.Gate = plane.Gate;
            wcfPlane.Departure = plane.Departure;
            wcfPlane.Arrival = plane.Arrival;
            wcfPlane.InteriorId = plane.InteriorId;
            wcfPlane.ReservedFirstClassSeats = plane.ReservedFirstClassSeats;
            wcfPlane.ReservedNormalSeats = plane.ReservedNormalSeats;
            wcfPlane.RouteId = plane.RouteId;
            return wcfPlane;
        }

        private WCFPlaneInterior ConvertPlaneInteriorToWCFPlaneInterior(PlaneInterior planeInterior)
        {
            WCFPlaneInterior wcfPlaneInterior = new WCFPlaneInterior();
            wcfPlaneInterior.InteriorId = planeInterior.InteriorId;
            wcfPlaneInterior.PlaneModel = planeInterior.PlaneModel;
            wcfPlaneInterior.TotalNormalSeats = planeInterior.TotalNormalSeats;
            wcfPlaneInterior.TotalFirstClassSeats = planeInterior.TotalFirstClassSeats;
            return wcfPlaneInterior;

        }

        private PlaneInterior ConvertWCFPlaneInteriorToPlaneInterior(WCFPlaneInterior wcfPlaneInterior)
        {
            PlaneInterior planeInterior = new PlaneInterior();
            planeInterior.InteriorId = wcfPlaneInterior.InteriorId;
            planeInterior.TotalNormalSeats = wcfPlaneInterior.TotalNormalSeats;
            planeInterior.TotalFirstClassSeats = wcfPlaneInterior.TotalFirstClassSeats;
            return planeInterior;
        }

        private WCFRoute ConvertRouteToWCFRoute(Route route)
        {
            WCFRoute wcfRoute = new WCFRoute();
            wcfRoute.RouteId = route.RouteId;
            wcfRoute.FromDestination = route.FromDestination;
            wcfRoute.ToDestination = route.ToDestination;
            wcfRoute.Time = route.Time;
            wcfRoute.Price = route.Price;
            return wcfRoute;
        }

        private Route ConvertWCFRouteToRoute(WCFRoute wcfRoute)
        {
            Route route = new Route();
            route.RouteId = wcfRoute.RouteId;
            route.FromDestination = wcfRoute.FromDestination;
            route.ToDestination = wcfRoute.ToDestination;
            route.Time = wcfRoute.Time;
            route.Price = wcfRoute.Price;
            return route;
        }

        #endregion
    }
}
